package com.esfak47.mediafetcher.app.service.impl;

import com.esfak47.common.PageResult;
import com.esfak47.mediafetcher.app.entity.MediaEs;
import com.esfak47.mediafetcher.app.repository.MediaEsRepository;
import com.esfak47.mediafetcher.core.domain.Media;
import com.esfak47.mediafetcher.core.service.MediaStorage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tony
 * @date 2018/5/6
 */
@Service(value = "mediaStorage")
public class MediaEsStorage implements MediaStorage {
    @Resource
    private MediaEsRepository mediaEsRepository;

    private static Media toMedia(MediaEs mediaEs) {
        return Media.builder().categories(mediaEs.getCategories())
            .commentCount(mediaEs.getCommentCount())
            .viewCount(mediaEs.getViewCount())
            .duration(mediaEs.getDuration())
            .ext(mediaEs.getExt())
            .extractorKey(mediaEs.getExtractorKey())
            .fileName(mediaEs.getFileName())
            .fullTitle(mediaEs.getFullTitle())
            .tags(mediaEs.getTags())
            .formatId(mediaEs.getFormatId())
            .hd(mediaEs.getHd())
            .thumbnail(mediaEs.getThumbnail())
            .thumbnails(mediaEs.getThumbnails())
            .rating(mediaEs.getRating())
            .url(mediaEs.getUrl())
            .id(mediaEs.getId())
            .webpageUrl(mediaEs.getWebpageUrl())
            .build();

    }

    private static MediaEs toMedia(Media mediaEs) {
        return MediaEs.builder().categories(mediaEs.getCategories())
            .commentCount(mediaEs.getCommentCount())
            .viewCount(mediaEs.getViewCount())
            .duration(mediaEs.getDuration())
            .ext(mediaEs.getExt())
            .extractorKey(mediaEs.getExtractorKey())
            .fileName(mediaEs.getFileName())
            .fullTitle(mediaEs.getFullTitle())
            .tags(mediaEs.getTags())
            .formatId(mediaEs.getFormatId())
            .hd(mediaEs.getHd())
            .thumbnail(mediaEs.getThumbnail())
            .thumbnails(mediaEs.getThumbnails())
            .rating(mediaEs.getRating())
            .url(mediaEs.getUrl())
            .id(mediaEs.getId())
            .webpageUrl(mediaEs.getWebpageUrl())
            .build();

    }

    @Override
    public void save(Media media) {
        MediaEs mediaEs = toMedia(media);
        mediaEsRepository.save(mediaEs);
    }

    @Override
    public Media getById(String id) {
        MediaEs byId = mediaEsRepository.findOne(id);
        if (byId != null) {
            return toMedia(byId);
        }
        return null;
    }

    @Override
    public void delete(String id) {
        mediaEsRepository.delete(id);
    }

    @Override
    public PageResult<Media> getPage(int page, int pageSize) {
        Page<MediaEs> all = mediaEsRepository.findAll(new PageRequest(page, pageSize));
        Page<Media> map = all.map(MediaEsStorage::toMedia);
        return PageResult.createPageResult(page, pageSize, map.getTotalElements(), map.getContent());
    }
}
